package com.scvtc.service.party.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.party.PartyBaseDto;
import com.scvtc.domain.entity.party.PartyBase;
import com.scvtc.domain.entity.party.PartyInparty;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.domain.vo.party.PartyBaseVo;
import com.scvtc.mapper.party.PartyBaseMapper;
import com.scvtc.service.party.PartyBaseService;
import com.scvtc.utils.BeanCopyUtils;
import com.scvtc.utils.DictionaryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("partyBaseService")
public class PartyBaseServiceImpl extends ServiceImpl<PartyBaseMapper, PartyBase> implements PartyBaseService {

    @Autowired
    private DictionaryUtil dictionaryUtil;

    @Autowired
    private PartyInpartyServiceImpl service;

    @Override
    public ResResult<PageVo<List<PartyBase>>> getAllPartyInfo(Long current, Long size, PartyBaseDto partyBaseDto) {

        Page<PartyBase> page = getBaseMapper().getAllInfo(new Page<>(current, size), partyBaseDto);
        // 拷贝bean
        List<PartyBaseVo> partyBaseVos = BeanCopyUtils.copyBean(page.getRecords(), PartyBaseVo.class);

        List<PartyBaseVo> testLi=new ArrayList<>();
        PartyBaseVo partyBaseVo=new PartyBaseVo();
        partyBaseVo.setId(10);
        testLi.add(partyBaseVo);

        return ResResult.noMsgSuccess(new PageVo<>(page.getRecords(), page.getTotal()));
    }

    @Override
    public ResResult<?> upDataPartInfo(PartyBaseDto partyBaseDto) {
        // 查询原来学生如果信息重复就提示错误
        PartyBase partyBase = BeanCopyUtils.copyBean(partyBaseDto, PartyBase.class);
        if (getBaseMapper().upDataPartInfo(partyBase)!=-1){
            return ResResult.success();
        }

        // 修改失败
//        if (updateById(partyBase)) {
////            dictionaryUtil.refreshData(partyBase, STUDENT_BASE, OpType.UPDATE);
//            return ResResult.success();
//        }
        // 发回信息错误
        return ResResult.fail();
    }

    @Override
    public ResResult<?> addPartInfo(PartyBaseDto partyBaseDto) {
        PartyBase partyBase = BeanCopyUtils.copyBean(partyBaseDto, PartyBase.class);


       boolean result=  save(partyBase);

        PartyInparty partyInparty=new PartyInparty();

//        partyInparty.setPartyId(1);
        partyInparty.setInPartyActivistsReport(partyBase.getPartyActivistsDate());
        partyInparty.setInPartyFormalReport(partyBase.getInPartyFormalReport());
        partyInparty.setInPartyPetition(partyBase.getInPartyPetition());
        partyInparty.setInPartyReallyReport(partyBase.getInPartyReallyReport());
        partyInparty.setInPartyRegister(partyBase.getInPartyRegister());
        partyInparty.setInPartyReport(partyBase.getInPartyReport());
//
//        getBaseMapper().insert(partyBase);


        if (result){
          int id=  this.getBaseMapper().selectById(partyBase).getId();
          partyInparty.setPartyId(id);
            if (service.save(partyInparty)){
                return ResResult.success();
            }
        }
        return ResResult.fail();

    }
}
